package com.menghai.parser;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.visitors.TextExtractingVisitor;

public class Main {
    private static String ENCODE = "GBK";
    private static void message( String szMsg ) {
        try{
        	System.out.println(new String(szMsg.getBytes(ENCODE), System.getProperty("file.encoding"))); 
        	} catch(Exception e ){}
    }
    public static String openFile( String szFileName ) {
        try {
            BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream( new File(szFileName)), ENCODE) );
            String szContent="";
            String szTemp;
            
            while ( (szTemp = bis.readLine()) != null) {
                szContent+=szTemp+"\n";
            }
            bis.close();
            return szContent;
        }
        catch( Exception e ) {
            return "";
        }
    }
    
   public static void main(String[] args) {
        
        String szContent = openFile( "E:/HTMLParserTester.html");
        
        try{
            //Parser parser = Parser.createParser(szContent, ENCODE);
            Parser parser = new Parser( szContent );
           //Parser parser = new Parser( (HttpURLConnection) (new URL("http://127.0.0.1:8080/HTMLParserTester.html")).openConnection() );
        
            //TextExtractingVisitor visitor = new TextExtractingVisitor();
           // parser.visitAllNodesWith(visitor);
           // String textInPage = visitor.getExtractedText();
            for (NodeIterator i = parser.elements (); i.hasMoreNodes(); ) {
                Node node = i.nextNode();
                message("getText:"+node.getText());
                message("getPlainText:"+node.toPlainTextString());
                message("toHtml:"+node.toHtml());
                message("toHtml(true):"+node.toHtml(true));
                message("toHtml(false):"+node.toHtml(false));
                message("toString:"+node.toString());
                message("=================================================");
            }  
            //message(textInPage);
            
            
            
        }
        catch( Exception e ) {            
        }
    }
}
